package com.xdy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdy.reggie.common.CustomException;
import com.xdy.reggie.entity.Category;
import com.xdy.reggie.entity.Dish;
import com.xdy.reggie.entity.SetMeal;
import com.xdy.reggie.mapper.CategoryMapper;
import com.xdy.reggie.service.CategoryService;
import com.xdy.reggie.service.DishService;
import com.xdy.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private DishService dishService;

    @Resource
    private SetMealService setMealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0){
            //分类下存在菜品，不能删除，抛出业务异常
            throw new CustomException("该分类下存在菜品，删除失败！");
        }
        LambdaQueryWrapper<SetMeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(SetMeal::getCategoryId,id);
        int count2 = setMealService.count(setMealLambdaQueryWrapper);
        if (count2 > 0){
            //分类下存在套餐，不能删除，抛出业务异常
            throw new CustomException("该分类下存在套餐，删除失败！");
        }
        baseMapper.deleteById(id);
    }
}
